#include "common.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "xml.h"

#define MEMBER(s,m) (s->m)

extern struct xml *mode;

static int
set_condition(struct xml *x, char *dst, int size)
{
    if (x == NULL || dst == NULL)
    {
        return (-1);
    }

    char member_name[128];
    char member_type[4];
    char member_size[8];

    struct xml *tmp = NULL;
    if ((tmp = get_first_xml_object(x, "name")) != NULL && tmp->type == XML_ELEMENT)
    {
        snprintf(member_name, sizeof(member_name), "%s", tmp->value);
    }

    if ((tmp = get_first_xml_object(x, "type")) != NULL && tmp->type == XML_ELEMENT)
    {
        snprintf(member_type, sizeof(member_type), "%s", tmp->value);
    }

    if ((tmp = get_first_xml_object(x, "size")) != NULL && tmp->type == XML_ELEMENT)
    {
        snprintf(member_size, sizeof(member_size), "%s", tmp->value);
    }

    if (strlen(dst) == 0)
    {
        snprintf(dst, size, "name=%s and type=%s and size=%s", member_name, member_type, member_size);
    }
    else
    {
        char tmp[2048];
        snprintf(tmp, sizeof(tmp), "%s and name=%s and type=%s and size=%s", dst, member_name, member_type, member_size);
        snprintf(dst, size, "%s", tmp);
    }
    return (0);
}

int
select_mode(const char *mode_name, void *m)
{
    struct xml *x = NULL;
    if ((x = get_mode_xml(mode, mode_name)) == NULL)
    {
        return (-1);
    }

    struct xml *class_name = NULL;
    if ((class_name = get_first_xml_object(x, "class-name")) == NULL || class_name->type != XML_ELEMENT)
    {
        return (-1);
    }

    char sql[4096];
    char condition[2048];
    memset(sql, 0, sizeof(sql));
    memset(condition, 0, sizeof(condition));
    struct xml *member = NULL;
    while ((member = get_xml_by_brothers(mode, member, "class-member")) != NULL)
    {
        struct xml *tmp = NULL;
        tmp = member->child;
        set_condition(member, condition, sizeof(condition));

        while (tmp != NULL)
        {
           printf("member->%s = %s\n", tmp->key, tmp->value);
           tmp = tmp->next;
        }
    }
    snprintf(sql, sizeof(sql), "select * from %s where %s", class_name->value, condition);
    printf("sql = %s\n", sql);

    return (0);
}

/* key-value对内存申请 */
struct key_value_pair *
key_value_pair_malloc(void)
{
    struct key_value_pair *key_value = NULL;
    if ((key_value = (struct key_value_pair *)malloc(sizeof(struct key_value_pair))) != NULL)
    {
        key_value->next = NULL;
        key_value->key = NULL;
        key_value->value = NULL;
    }
    return (key_value);
}

void
destroy_key_value(struct key_value_pair *kv)
{
    while (kv != NULL)
    {
        struct key_value_pair *free_node = NULL;
        free_node = kv;
        kv = free_node->next;
        if (free_node->key != NULL)
        {
            free(free_node->key);
        }
        if (free_node->value != NULL)
        {
            free(free_node->value);
        }
        free(free_node);
    }
}
